<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pagevisibilitymonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_dom_pagevisibilitymonitor.js.html">pagevisibilitymonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This event monitor wraps the Page Visibility API.
<a name="line17"></a> * @see http://www.w3.org/TR/page-visibility/
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.labs.dom.PageVisibilityEvent&#39;);
<a name="line21"></a>goog.provide(&#39;goog.labs.dom.PageVisibilityMonitor&#39;);
<a name="line22"></a>goog.provide(&#39;goog.labs.dom.PageVisibilityState&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.vendor&#39;);
<a name="line26"></a>goog.require(&#39;goog.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.memoize&#39;);
<a name="line31"></a>
<a name="line32"></a>goog.scope(function() {
<a name="line33"></a>var dom = goog.labs.dom;
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * The different visibility states.
<a name="line38"></a> * @enum {string}
<a name="line39"></a> */
<a name="line40"></a>dom.PageVisibilityState = {
<a name="line41"></a>  HIDDEN: &#39;hidden&#39;,
<a name="line42"></a>  VISIBLE: &#39;visible&#39;,
<a name="line43"></a>  PRERENDER: &#39;prerender&#39;,
<a name="line44"></a>  UNLOADED: &#39;unloaded&#39;
<a name="line45"></a>};
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * This event handler allows you to catch page visibility change events.
<a name="line51"></a> * @param {!goog.dom.DomHelper=} opt_domHelper
<a name="line52"></a> * @constructor
<a name="line53"></a> * @extends {goog.events.EventTarget}
<a name="line54"></a> * @final
<a name="line55"></a> */
<a name="line56"></a>dom.PageVisibilityMonitor = function(opt_domHelper) {
<a name="line57"></a>  dom.PageVisibilityMonitor.base(this, &#39;constructor&#39;);
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * @private {!goog.dom.DomHelper}
<a name="line61"></a>   */
<a name="line62"></a>  this.domHelper_ = opt_domHelper || goog.dom.getDomHelper();
<a name="line63"></a>
<a name="line64"></a>  /**
<a name="line65"></a>   * @private {?string}
<a name="line66"></a>   */
<a name="line67"></a>  this.eventType_ = this.getBrowserEventType_();
<a name="line68"></a>
<a name="line69"></a>  // Some browsers do not support visibilityChange and therefore we don&#39;t bother
<a name="line70"></a>  // setting up events.
<a name="line71"></a>  if (this.eventType_) {
<a name="line72"></a>    /**
<a name="line73"></a>     * @private {goog.events.Key}
<a name="line74"></a>     */
<a name="line75"></a>    this.eventKey_ = goog.events.listen(this.domHelper_.getDocument(),
<a name="line76"></a>        this.eventType_, goog.bind(this.handleChange_, this));
<a name="line77"></a>  }
<a name="line78"></a>};
<a name="line79"></a>goog.inherits(dom.PageVisibilityMonitor, goog.events.EventTarget);
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * @return {?string} The visibility change event type, or null if not supported.
<a name="line84"></a> *     Memoized for performance.
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>dom.PageVisibilityMonitor.prototype.getBrowserEventType_ =
<a name="line88"></a>    goog.memoize(function() {
<a name="line89"></a>  var isSupported = this.isSupported();
<a name="line90"></a>  var isPrefixed = this.isPrefixed_();
<a name="line91"></a>
<a name="line92"></a>  if (isSupported) {
<a name="line93"></a>    return isPrefixed ? goog.dom.vendor.getPrefixedEventType(
<a name="line94"></a>        goog.events.EventType.VISIBILITYCHANGE) :
<a name="line95"></a>        goog.events.EventType.VISIBILITYCHANGE;
<a name="line96"></a>  } else {
<a name="line97"></a>    return null;
<a name="line98"></a>  }
<a name="line99"></a>});
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * @return {?string} The browser-specific document.hidden property.  Memoized
<a name="line104"></a> *     for performance.
<a name="line105"></a> * @private
<a name="line106"></a> */
<a name="line107"></a>dom.PageVisibilityMonitor.prototype.getHiddenPropertyName_ = goog.memoize(
<a name="line108"></a>    function() {
<a name="line109"></a>      return goog.dom.vendor.getPrefixedPropertyName(
<a name="line110"></a>          &#39;hidden&#39;, this.domHelper_.getDocument());
<a name="line111"></a>    });
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * @return {boolean} Whether the visibility API is prefixed.
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>dom.PageVisibilityMonitor.prototype.isPrefixed_ = function() {
<a name="line119"></a>  return this.getHiddenPropertyName_() != &#39;hidden&#39;;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * @return {?string} The browser-specific document.visibilityState property.
<a name="line125"></a> *     Memoized for performance.
<a name="line126"></a> * @private
<a name="line127"></a> */
<a name="line128"></a>dom.PageVisibilityMonitor.prototype.getVisibilityStatePropertyName_ =
<a name="line129"></a>    goog.memoize(function() {
<a name="line130"></a>  return goog.dom.vendor.getPrefixedPropertyName(
<a name="line131"></a>      &#39;visibilityState&#39;, this.domHelper_.getDocument());
<a name="line132"></a>});
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * @return {boolean} Whether the visibility API is supported.
<a name="line137"></a> */
<a name="line138"></a>dom.PageVisibilityMonitor.prototype.isSupported = function() {
<a name="line139"></a>  return !!this.getHiddenPropertyName_();
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * @return {boolean} Whether the page is visible.
<a name="line145"></a> */
<a name="line146"></a>dom.PageVisibilityMonitor.prototype.isHidden = function() {
<a name="line147"></a>  return !!this.domHelper_.getDocument()[this.getHiddenPropertyName_()];
<a name="line148"></a>};
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * @return {?dom.PageVisibilityState} The page visibility state, or null if
<a name="line153"></a> *     not supported.
<a name="line154"></a> */
<a name="line155"></a>dom.PageVisibilityMonitor.prototype.getVisibilityState = function() {
<a name="line156"></a>  if (!this.isSupported()) {
<a name="line157"></a>    return null;
<a name="line158"></a>  }
<a name="line159"></a>  return this.domHelper_.getDocument()[this.getVisibilityStatePropertyName_()];
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Handles the events on the element.
<a name="line165"></a> * @param {goog.events.BrowserEvent} e The underlying browser event.
<a name="line166"></a> * @private
<a name="line167"></a> */
<a name="line168"></a>dom.PageVisibilityMonitor.prototype.handleChange_ = function(e) {
<a name="line169"></a>  var state = this.getVisibilityState();
<a name="line170"></a>  var visibilityEvent = new dom.PageVisibilityEvent(
<a name="line171"></a>      this.isHidden(), /** @type {dom.PageVisibilityState} */ (state));
<a name="line172"></a>  this.dispatchEvent(visibilityEvent);
<a name="line173"></a>};
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>/** @override */
<a name="line177"></a>dom.PageVisibilityMonitor.prototype.disposeInternal = function() {
<a name="line178"></a>  goog.events.unlistenByKey(this.eventKey_);
<a name="line179"></a>  dom.PageVisibilityMonitor.base(this, &#39;disposeInternal&#39;);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * A page visibility change event.
<a name="line186"></a> * @param {boolean} hidden Whether the page is hidden.
<a name="line187"></a> * @param {goog.labs.dom.PageVisibilityState} visibilityState A more detailed
<a name="line188"></a> *     visibility state.
<a name="line189"></a> * @constructor
<a name="line190"></a> * @extends {goog.events.Event}
<a name="line191"></a> * @final
<a name="line192"></a> */
<a name="line193"></a>dom.PageVisibilityEvent = function(hidden, visibilityState) {
<a name="line194"></a>  dom.PageVisibilityEvent.base(
<a name="line195"></a>      this, &#39;constructor&#39;, goog.events.EventType.VISIBILITYCHANGE);
<a name="line196"></a>
<a name="line197"></a>  /**
<a name="line198"></a>   * Whether the page is hidden.
<a name="line199"></a>   * @type {boolean}
<a name="line200"></a>   */
<a name="line201"></a>  this.hidden = hidden;
<a name="line202"></a>
<a name="line203"></a>  /**
<a name="line204"></a>   * A more detailed visibility state.
<a name="line205"></a>   * @type {dom.PageVisibilityState}
<a name="line206"></a>   */
<a name="line207"></a>  this.visibilityState = visibilityState;
<a name="line208"></a>};
<a name="line209"></a>goog.inherits(dom.PageVisibilityEvent, goog.events.Event);
<a name="line210"></a>
<a name="line211"></a>});  // goog.scope
</pre>


</body>
</html>
